﻿//作者：       minjie.zhou
// 创建时间：   2012/12/11 0:00:07
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using YDNoteOpenAPI4N.YDConsumer;
using mshtml;

namespace YDNoteOpenAPI4N.Test.WPF
{
    /// <summary>
    /// Page1.xaml 的交互逻辑
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }
       
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
 
        }

        void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
        {
            var wb = sender as WebBrowser;
            var doc = wb.Document as HTMLDocument;
            if(doc.body==null)
                return;
            if (doc.body.innerHTML.Contains("记住我的登录状态"))
            {
                IHTMLElement user = doc.getElementById("username");
                user.setAttribute("value",this.textBox1.Text.Trim());
                IHTMLElement pwd = doc.getElementById("password");
                pwd.setAttribute("value", this.textBox2.Text.Trim());
                var inputs = doc.getElementsByTagName("input");
                foreach (IHTMLElement input in inputs)
                {
                    input.click(); 
                }
            }
            else if (doc.body.innerHTML.Contains("访问你的有道云笔记账号"))//模拟点击授权
            {
                IHTMLElement element = doc.getElementById("auth-btn");
                element.click();
            }
            else if (doc.body.innerHTML.Contains("请在程序相应位置输入授权码对AgileToDo进行授权"))
            {
                var list = doc.getElementsByTagName("font");
                var varCode = "";
                foreach (var item in list)
                {
                    var font = item as IHTMLElement;
                    OAuthHelper.VerifierCode = font.innerText.Trim();
                }
                var grantedAccess = OAuthHelper.Consumer.ProcessUserAuthorization(OAuthHelper.RequestToken, OAuthHelper.VerifierCode);
                string accessToken = grantedAccess.AccessToken;
                OAuthHelper.AccessToken = accessToken;

                var p2 = new Page2();

                var nvs = NavigationService.GetNavigationService(this);
                nvs.Navigated += p2.NavigationService_Navigated;
                nvs.Navigate(p2);
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string consumerKey = YDAuthBaseInfo.ConsumerKey;
            string consumerSecret = YDAuthBaseInfo.ConsumerSecret;
            OAuthHelper.TokenManager = new YDTokenManager(consumerKey, consumerSecret);
            OAuthHelper.Consumer = new YDDesktopConsumer(YDAuthBaseInfo.ServiceDescription, OAuthHelper.TokenManager);

            wb.LoadCompleted += (webBrowser1_LoadCompleted);
            wb.Navigate(YDDesktopConsumer.RequestAuthorization(OAuthHelper.Consumer, out OAuthHelper.RequestToken));

         
        }
    }
}
